﻿
// 2018/6/20: 首个记录
// 2018/8/14: 增加getTitle，支持significantCount
// 2018/12/29: 改pragma once
// 2019/10/15: 修改include

#ifndef GNSSIMU_SAMPLE_V4_H
#define GNSSIMU_SAMPLE_V4_H

#include "spadas.h"

namespace gnssimu_sample_v4
{
	using namespace spadas;

	struct GnssImuSample
	{
		GlobalTimestamp timeStamp;

		OptionalDouble longitude; // deg-E
		OptionalDouble latitude; // deg-N
		OptionalDouble altitude; // m
		OptionalDouble speed; // kph
		OptionalDouble orientation; // deg-CCW, 0:north, -180~180

		OptionalDouble pitch; // deg-down
		OptionalDouble roll; // deg-CW
		OptionalDouble yawRate; // deg-left
		OptionalDouble ax; // m/s2
		OptionalDouble ay; // m/s2
		OptionalDouble az; // m/s2，up-direction

		Optional<Time> gnssTime;
		OptionalInt gnssTimeMS;

		static String getProtocolName(UInt channel)
		{
			return (String)"gnssimu-sample-v4@" + channel;
		}

		static String getTitle()
		{
			return "Longitude[°],Latitude[°],Altitude[m],Speed[KPH],Orientation[°],Pitch angle[°],Roll angle[°],Yaw rate[°/s],Acceleration-x[m/s2],Acceleration-y[m/s2],Acceleration-z[m/s2],Year,Month,Day,Hour,Minute,Second,Millisecond";
		}

		Bool fromGeneralSample(GeneralSample sample)
		{
			if (sample.values.size() != 18) return FALSE;

			timeStamp = sample.timeStamp;

			longitude = sample.values[0];
			latitude = sample.values[1];
			altitude = sample.values[2];
			speed = sample.values[3];
			orientation = sample.values[4];
			pitch = sample.values[5];
			roll = sample.values[6];
			yawRate = sample.values[7];
			ax = sample.values[8];
			ay = sample.values[9];
			az = sample.values[10];

			if (sample.values[11].valid &&
				sample.values[12].valid &&
				sample.values[13].valid &&
				sample.values[14].valid &&
				sample.values[15].valid &&
				sample.values[16].valid &&
				sample.values[17].valid)
			{
				gnssTime = Time((UInt)sample.values[11].value,
					(UInt)sample.values[12].value,
					(UInt)sample.values[13].value,
					(UInt)sample.values[14].value,
					(UInt)sample.values[15].value,
					(UInt)sample.values[16].value);
				gnssTimeMS = (Int)sample.values[17].value;
			}
			else
			{
				gnssTime = Optional<Time>();
				gnssTimeMS = OptionalInt();
			}

			return TRUE;
		}
		GeneralSample toGeneralSample()
		{
			GeneralSample sample;
			sample.timeStamp = timeStamp;
			sample.values = Array<OptionalDouble>(18);
			OptionalDouble *data = sample.values.data();

			data[0] = longitude;
			data[1] = latitude;
			data[2] = altitude;
			data[3] = speed;
			data[4] = orientation;
			data[5] = pitch;
			data[6] = roll;
			data[7] = yawRate;
			data[8] = ax;
			data[9] = ay;
			data[10] = az;
			data[11] = gnssTime.isValid() ? gnssTime.refValue().year : OptionalDouble();
			data[12] = gnssTime.isValid() ? gnssTime.refValue().month : OptionalDouble();
			data[13] = gnssTime.isValid() ? gnssTime.refValue().day : OptionalDouble();
			data[14] = gnssTime.isValid() ? gnssTime.refValue().hour : OptionalDouble();
			data[15] = gnssTime.isValid() ? gnssTime.refValue().minute : OptionalDouble();
			data[16] = gnssTime.isValid() ? gnssTime.refValue().second : OptionalDouble();
			data[17] = gnssTimeMS.valid ? gnssTimeMS.value : OptionalDouble();

			sample.significantCount = 18;
			return sample;
		}
	};
}

#endif
